using EveApiLib;
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;

namespace EveSkillPocket
{
    public class EVESettings
    {
        private int cfgAutoUpdateInterval = 2;
        private bool cfgAutoUpdate = true;
        private bool cfgNotifyIcon = true;
        private int cfgWarn = 15;
        private bool cfgToday = false;
        private EveAPIConfig eaConfig = new EveAPIConfig();
        public const string Developers = "Johnny Dread\r\nMike Starfury\r\nKazie Kama";
        private string filename;

        public EVESettings()
        {
            if (!this.eaConfig.LoadFromFile(fCharacter.GetDir() + "character.xml"))
            {
                this.eaConfig = null;
            }
        }

        public bool loadConfiguration()
        {
            string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            return this.loadConfiguration(directoryName + @"\eveskillpocket.conf");
        }

        public bool loadConfiguration(string filename)
        {
            this.filename = filename;
            try
            {
                StreamReader reader = new StreamReader(filename);
                string s = null;
                while ((s = reader.ReadLine()) != null)
                {
                    if (s.StartsWith("warn"))
                    {
                        s = s.Split(new char[] { '=' })[1];
                        this.cfgWarn = int.Parse(s);
                    }
                    else if (s.StartsWith("updateinterval"))
                    {
                        s = s.Split(new char[] { '=' })[1];
                        this.cfgAutoUpdateInterval = int.Parse(s);
                        continue;
                    }
                    else if (s.StartsWith("notifyicon"))
                    {
                        s = s.Split(new char[] { '=' })[1];
                        this.cfgNotifyIcon = bool.Parse(s);
                    }
                    else if (s.StartsWith("today"))
                    {
                        s = s.Split(new char[] { '=' })[1];
                        this.cfgToday = bool.Parse(s);
                    }
                    else if (s.StartsWith("autoupdate"))
                    {
                        s = s.Split(new char[] { '=' })[1];
                        this.cfgAutoUpdate = bool.Parse(s);
                    }
                }
                reader.Close();
                reader = null;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool saveConfiguration()
        {
            if (this.filename != null)
            {
                return this.saveConfiguration(this.filename);
            }
            MessageBox.Show("Invalid Filename", "Error");
            return false;
        }

        public bool saveConfiguration(string filename)
        {
            try
            {
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                StreamWriter writer = new StreamWriter(filename);
                writer.WriteLine("warn=" + this.cfgWarn);
                writer.WriteLine("updateinterval=" + this.cfgAutoUpdateInterval);
                writer.WriteLine("autoupdate=" + this.cfgAutoUpdate);
                writer.WriteLine("notifyicon=" + this.cfgNotifyIcon);
                writer.WriteLine("today=" + this.cfgToday);
                writer.Close();
                writer = null;
                return true;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.GetType().FullName, "Error");
                return false;
            }
        }

        public int AutoUpdateInterval
        {
            get
            {
                return this.cfgAutoUpdateInterval;
            }
            set
            {
                this.cfgAutoUpdateInterval = value;
            }
        }

        public EveAPIConfig Config
        {
            get
            {
                return this.eaConfig;
            }
        }

        public bool NotifyIcon
        {
            get
            {
                return this.cfgNotifyIcon;
            }
            set
            {
                this.cfgNotifyIcon = value;
            }
        }

        public bool Today
        {
            get
            {
                return this.cfgToday;
            }
            set
            {
                this.cfgToday = value;
            }
        }

        public bool AutoUpdate
        {
            get
            {
                return this.cfgAutoUpdate;
            }
            set
            {
                this.cfgAutoUpdate = value;
            }
        }

        public static string Version
        {
            get
            {
                System.Version version = Assembly.GetExecutingAssembly().GetName().Version;
                return string.Concat(new object[] { "", version.Major, ".", version.Minor, ".", version.Build });
            }
        }

        public int Warning
        {
            get
            {
                return this.cfgWarn;
            }
            set
            {
                this.cfgWarn = value;
            }
        }
    }
}
